﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Threading;
using System.Drawing;

namespace WXTao
{
    class Weixin
    {
        [DllImport("user32.dll", SetLastError = true)] static extern bool PostMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);
        [DllImport("user32.dll")] private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
        [DllImport("user32.dll")] private static extern bool SetForegroundWindow(IntPtr hWnd);
        [DllImport("user32.dll")] private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        [DllImport("user32.dll")] private static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
        [DllImport("user32.dll")] private static extern bool SetCursorPos(int X, int Y);
        [DllImport("user32.dll")] private static extern void mouse_event(int dwFlags, int dx, int dy, int dwData, int dwExtraInfo);
        [DllImport("user32.dll")] private static extern void keybd_event(byte bVk, byte bScan, uint dwFlags, uint dwExtraInfo);
        [DllImport("user32.dll")] private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndlnsertAfter, int X, int Y, int cx, int cy, uint Flags);
        [DllImport("user32.dll", EntryPoint = "FindWindowEx")]
        private extern static IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

        [DllImport("user32.dll", EntryPoint = "PostMessageA", SetLastError = true)]
        public static extern int PostMessage(IntPtr hWnd, int Msg, Keys wParam, int lParam);
        [DllImport("user32.dll")] private static extern int GetWindowTextW(IntPtr hWnd, [MarshalAs(UnmanagedType.LPWStr)]StringBuilder lpString, int nMaxCount);
        [DllImport("user32.dll")] private static extern int GetClassNameW(IntPtr hWnd, [MarshalAs(UnmanagedType.LPWStr)]StringBuilder lpString, int nMaxCount);

        [DllImport("user32.dll", EntryPoint = "keybd_event", SetLastError = true)]
        public static extern void keybd_event(Keys bVk, byte bScan, uint dwFlags, uint dwExtraInfo);
        public const int KEYEVENTF_KEYUP = 2;

        public struct RECT
        {
            public int Left; //最左坐标
            public int Top; //最上坐标
            public int Right; //最右坐标
            public int Bottom; //最下坐标
        }

        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool GetWindowRect(IntPtr hWnd, ref RECT lpRect);

        public struct Point
        {
            public int x;
            public int y;
        }
        //public struct PLACEMENT
        //{
        //    int Length;
        //    int flags;
        //    int showCmd;
        //    Point ptMinPosition;
        //    Point ptMaxPosition;
        //    RECT rcNormalPosition;
        //}
        //[DllImport("user32.dll")] static extern bool GetWindowPlacement(IntPtr hWnd, ref PLACEMENT lpRect);

        //ShowWindow参数
        private const int SW_SHOWNORMAL = 1;
        private const int SW_RESTORE = 9;
        private const int SW_SHOWNOACTIVATE = 4;
        //SendMessage参数
        private const int WM_KEYDOWN = 0X100;
        private const int WM_KEYUP = 0X101;
        private const int WM_SYSCHAR = 0X106;
        private const int WM_SYSKEYUP = 0X105;
        private const int WM_SYSKEYDOWN = 0X104;
        private const int WM_CHAR = 0X102;

        private const int BM_CLICK = 0xF5;
        private uint WM_LBUTTONDOWN = 0x201;
        private uint WM_LBUTTONUP = 0x202;


        private int origin_width = 688;
        private int origin_height = 800;

        private Point origin_send_point;//发送按钮
        private Point origin_edit_point;//编辑框

        private string windowName = "";

        private IntPtr myIntPtr ;

        public Weixin(string name)
        {
            windowName = name;
            origin_send_point = new Point();
            origin_send_point.x = 600;
            origin_send_point.y = 775;

            origin_edit_point = new Point();
            origin_edit_point.x = 70;
            origin_edit_point.y = 720;
        }

        public Weixin(){
            origin_send_point = new Point();
            origin_send_point.x = 600;
            origin_send_point.y = 775;

            origin_edit_point = new Point();
            origin_edit_point.x = 70;
            origin_edit_point.y = 720;
        }

      
        public void setWindowName(string name)
        {
            windowName = name;
        }

        public bool isWindowExist()
        {
            StringBuilder className = new StringBuilder() ;
            myIntPtr = FindWindow(null, windowName);
            
            if ((int)myIntPtr == 0)
                return false;
            else
            {
                GetClassNameW(myIntPtr, className, 255);
                if(className.ToString().Equals("ChatWnd"))
                {
                    return true;
                }
                return false;
            }
        }

        private void showWindow(IntPtr handle)
        {
            ShowWindow(handle, SW_RESTORE);
            SetForegroundWindow(handle);
        }

        public void sendContent(string content,string end,string image_url)
        {
            if (!isWindowExist())
                return;
            RECT fx = new RECT();
            GetWindowRect(myIntPtr, ref fx);

            int width = fx.Right - fx.Left;
            int height = fx.Bottom - fx.Top;

            Point edit_point = new Point();
            Point send_point = new Point();

            //焦点移到输入框
            edit_point.x = origin_edit_point.x;
            edit_point.y = origin_edit_point.y;
            PostMessage(myIntPtr, WM_LBUTTONDOWN, 0, edit_point.x + (edit_point.y << 16));
            PostMessage(myIntPtr, WM_LBUTTONUP, 0, edit_point.x + (edit_point.y << 16));

            //复制图片到剪切板
            Copy_Image(image_url);
            Thread.Sleep(1000);
            //粘贴内容到输入框
            Paste_Click(myIntPtr);
            Thread.Sleep(1000);


            //焦点移到发送按钮
            send_point.x = width - (origin_width - origin_send_point.x);
            send_point.y = height - (origin_height - origin_send_point.y);
            PostMessage(myIntPtr, WM_LBUTTONDOWN, 0, send_point.x + (send_point.y << 16));
            PostMessage(myIntPtr, WM_LBUTTONUP, 0, send_point.x + (send_point.y << 16));
            Thread.Sleep(2500);

            //复制内容到剪切板
            Copy_Text(content);
            Thread.Sleep(500);
            //粘贴内容到输入框
            Paste_Click(myIntPtr);
            Thread.Sleep(500);

            //Copy_Text_End(content);
            //Thread.Sleep(500);
            ////粘贴内容到输入框
            //Paste_Click(myIntPtr);
            //Thread.Sleep(500);

            Copy_Text(end);
            Thread.Sleep(500);
            //粘贴内容到输入框
            Paste_Click(myIntPtr);
            Thread.Sleep(500);


            //焦点移到发送按钮
            send_point.x = width - (origin_width - origin_send_point.x);
            send_point.y = height - (origin_height - origin_send_point.y);
            PostMessage(myIntPtr, WM_LBUTTONDOWN, 0, send_point.x + (send_point.y << 16));
            PostMessage(myIntPtr, WM_LBUTTONUP, 0, send_point.x + (send_point.y << 16));

        }

        private void Paste_Click(IntPtr myIntPtr)
        {
            keybd_event(Keys.ControlKey, 0, 0, 0);
            Thread.Sleep(10);
            PostMessage(myIntPtr, WM_KEYDOWN, Keys.V, 0);
            Thread.Sleep(10);
            keybd_event(Keys.ControlKey, 0, KEYEVENTF_KEYUP, 0);
        }

        private void Copy_Text(string text)
        {
            
            Clipboard.SetDataObject(text, true);
        }


        //复制
        private void Copy_Text_One(string text)
        {
            string s = text.Substring(0, 1);
            Clipboard.SetDataObject(s, true);
        }

        private void Copy_Text_End(string text)
        {
            string s = text.Substring(1, text.Length-1);
            Clipboard.SetDataObject(s, true);
        }

        private void Copy_Image(string url)
        {
            if(url.Equals(""))
            {
                Clipboard.SetDataObject("", true);
                return;
            }
            if(!url.Contains("http:") && !url.Contains("https:"))
            {
                url = "http:" + url ;
            }
            Image image = Image.FromStream(System.Net.WebRequest.Create(url).GetResponse().GetResponseStream());
            Clipboard.SetImage(image);
        }
    }
}
